package cn.client.ui.view.main.user.avoidancePain

import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.AvoidancePainReq
import cn.client.data.userStateManager
import cn.client.ui.components.BaseTopBar
import cn.client.ui.components.SubmitButton
import cn.client.ui.view.main.mindfulness.HomeSection
import cn.client.ui.view.main.mindfulness.RatingBar
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

// 创建痛苦回避事项页面
@Composable
fun AvoidancePainCreate(
    navController: NavController,
    viewModel: AvoidancePainViewModel = hiltViewModel()
){
    var avoidanceLevel by remember { mutableIntStateOf(0) }
    var painLevel by remember { mutableIntStateOf(0) }
    var scene: String by remember { mutableStateOf("") }
    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when(it){
                is AvoidancePainViewModel.PageEvent.Success -> {
                    navController.navigateUp()
                }
                is AvoidancePainViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    Scaffold(
        topBar = {
            BaseTopBar(
                title = "创建痛苦回避事项",
                onClickArrowBack = {
                    coroutineScope.launch {
                        navController.navigateUp()
                    }
                }
            )
        }
    ) {
        Column(
            modifier = Modifier.padding(it)
        ) {
            /* 内容输入框 */
            HomeSection(title = "情景") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .padding(5.dp)
                ) {
                    TextField(
                        value = scene,
                        onValueChange = { str -> scene = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(5.dp)
                    )
                }
            }
            HomeSection(title = "回避程度") {
                Row(
                    modifier = Modifier.padding(15.dp)
                ) {
                    // 显示打分条
                    RatingBar(
                        maxRating = 10,
                        currentRating = avoidanceLevel,
                        onRatingChanged = { avoidanceLevel = it },
                        starsColor = Color.Magenta
                    )
                }
            }
            HomeSection(title = "痛苦程度") {
                Row(
                    modifier = Modifier.padding(15.dp)
                ) {
                    // 显示打分条
                    RatingBar(
                        maxRating = 10,
                        currentRating = painLevel,
                        onRatingChanged = { painLevel = it },
                        starsColor = Color.Magenta
                    )
                }
            }

            // 提交
            SubmitButton(
                onClickSubmitButton = {
                    val req = AvoidancePainReq(
                        uid = userId,
                        scene = scene,
                        avoidanceLevel = avoidanceLevel,
                        painLevel = painLevel
                    )
                    coroutineScope.launch {
                        viewModel.dispatch(AvoidancePainViewModel.PageIntent.Add(req))
                    }
                }
            )
        }
    }


}